package com.woniu.his.controller;

import com.woniu.his.entity.BillInfo;
import com.woniu.his.entity.IncomeRecord;
import com.woniu.his.service.IFinanceService;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
public class FinanceController {

    @Autowired
    private IFinanceService iFinanceService;

    @GetMapping("finance")
    @RequiresRoles("财务部")
    public String toSingleFinance(ModelMap modelMap) {
//        List<BillInfo> billInfos = iFinanceService.findAllBills();
//        System.out.println(billInfos + "finance请求");
//        modelMap.put("billInfos", billInfos);
        return "bills";
    }

    @RequestMapping(value = "singlefinance", method = RequestMethod.POST)
    @RequiresRoles("财务部")
    public String toFinance(Integer medicalId, ModelMap modelMap) {
        if (medicalId != null) {
            System.out.println("这个是........" + medicalId);
            List<BillInfo> billInfos = iFinanceService.findByMedicalIdPatientName(medicalId);
//            if (billInfos != null) {
//                for (BillInfo b : billInfos) {
//                    String patientName1 = b.getPatientName();
//                    int medicalId1 = b.getMedicalId();
//                    String incomeDetail = b.getPayProject();
//                    BigDecimal incomePrice = b.getPrice();
//                    int incomeCount = b.getCount();
//                    BigDecimal totalPrice = b.getTotalPrice();
//                    Date incomeTime = b.getPayTime();
//                    int status = b.getStatus();
//                    //将缴费记录插入数据库
//                    int i = iFinanceService.addIncomeRecord(patientName1, medicalId1, incomeDetail, incomePrice, incomeCount, totalPrice, incomeTime, status);
//                    System.out.println(i);
//                    if (i > 0) {
//                        iFinanceService.updateStatus();
//                    }
//                }
//            }
            modelMap.put("billInfos", billInfos);
        } else {
            modelMap.put("msg", "*请正确输入正确的查询信息*");
        }
        return "bills";
    }

    @GetMapping("income")
    @RequiresRoles("财务部")
    public String toIncomeRecord(ModelMap modelMap) {
        List<IncomeRecord> Records = iFinanceService.findIncomeRecord();
        System.out.println(Records + "income请求");
        BigDecimal totalIncome = iFinanceService.findTotalIncome();
        System.out.println(totalIncome + "income请求");
        modelMap.put("record", Records);
        modelMap.put("totalIncome", totalIncome);
        return "incomerecord";
    }

    //这是处理缴费的请求
    @GetMapping("updateIncomeInfo")
    @RequiresRoles("财务部")
    public String updateIncomeInfo(int payId, String patientName, int medicalId, String payProject, double price, int count, double totalPrice, ModelMap modelMap) {
        //根据id将缴费表中的状态更改
        System.out.println(payId + patientName + medicalId + payProject + price + count + totalPrice);
        String result = iFinanceService.updateIncomeInfo(payId, patientName, medicalId, payProject, price, count, totalPrice);
        if (result != null) {
            modelMap.put("msg", "操作成功");
        } else {
            modelMap.put("msg", "操作失败");
        }
        List<BillInfo> billInfos = iFinanceService.findByMedicalIdPatientName(medicalId);
        modelMap.put("billInfos", billInfos);
        return "bills";
    }
}
